home *** CD-ROM | disk | FTP | other *** search
- /* ===========================================================================
- Most code derived from demo code received from Smartcode Software, Inc.
- =========================================================================== */
-
- #include <Pilot.h>
- #include <SysEvtMgr.h>
- #include <StringMgr.h>
- #include <FeatureMgr.h>
- #include <SerialMgr.h>
- #include <SerialMgrNew.h>
-
- #include "DiscRemoteRes.h"
- #include "CommandStrings.h"
-
- // Comment out if compiling for the old serial manager
- // FIXME: Need to query for new serial manager and dynamically use correct calls?
-
-
- /* --------------------------------------------------------------------------- */
-
- #define version30 0x03000000 // PalmOS 3.0 version number
-
- /* --------------------------------------------------------------------------- */
-
- static UInt16 gRefNum;
- static UInt32 gUseNewSerialManager;
- static Char gTestBuffer[4096];
-
- /* --------------------------------------------------------------------------- */
-
- //#define kSerialLibName "SerIrCOMM Lib"
- #define kSerialLibName "Serial Library"
-
- static Err PrvSerLibFind(UInt16* refNumP)
- {
- if (gUseNewSerialManager) {
- *refNumP = 0x1234;
- return 0;
- }
- else {
- return SysLibFind(kSerialLibName, refNumP);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Err PrvSerOpen(UInt16 refNum, UInt32 portId, UInt32 baud, UInt16* newPortP)
- {
- if (gUseNewSerialManager) {
- return SrmOpen(portId, baud, newPortP);
- }
- else {
- return SerOpen(refNum, 0, baud);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Err PrvSerClose(UInt16 refNum)
- {
- if (gUseNewSerialManager) {
- return SrmClose(refNum);
- }
- else {
- return SerClose(refNum);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Err PrvSerReceiveCheck(UInt16 refNum, UInt32* numBytesP)
- {
- if (gUseNewSerialManager) {
- return SrmReceiveCheck(refNum, numBytesP);
- }
- else {
- return SerReceiveCheck(refNum, numBytesP);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- static UInt32 PrvSerReceive(UInt16 refNum, void* rcvBufP, UInt32 count, Int32 timeout, Err* errP)
- {
- if (gUseNewSerialManager) {
- return SrmReceive(refNum, rcvBufP, count, timeout, errP);
- }
- else {
- return SerReceive(refNum, rcvBufP, count, timeout, errP);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- static UInt32 PrvSerSend (UInt16 refNum, void* sndBufP, UInt32 count, Err* errP)
- {
- if (gUseNewSerialManager) {
- return SrmSend(refNum, sndBufP, count, errP);
- }
- else {
- return SerSend(refNum, sndBufP, count, errP);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void* GetObjectPtr(UInt16 id)
- {
- FormPtr form = FrmGetActiveForm();
- return FrmGetObjectPtr(form, FrmGetObjectIndex(form, id));
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void UpdateScrollBar(UInt16 textID, UInt16 scrollBarID)
- {
- UInt16 scrollPos;
- UInt16 textHeight;
- UInt16 fieldHeight;
- Int16 maxValue;
- FieldPtr textField;
- ScrollBarPtr scrollBar;
-
- textField = GetObjectPtr(textID);
- scrollBar = GetObjectPtr(scrollBarID);
-
- FldGetScrollValues(textField, &scrollPos, &textHeight, &fieldHeight);
-
- if (textHeight > fieldHeight)
- maxValue = textHeight - fieldHeight;
- else
- maxValue = scrollPos;
-
- if (scrollPos > maxValue)
- scrollPos = maxValue;
-
- SclSetScrollBar(scrollBar, scrollPos, 0, maxValue, fieldHeight - 1);
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void HandleTextScroll(Int16 linesToScroll, UInt16 textID, UInt16 scrollBarID)
- {
- UInt16 blankLines;
- FieldPtr textField;
- UInt16 scroll;
-
- textField = GetObjectPtr(textID);
-
- if (linesToScroll < 0) {
- blankLines = FldGetNumberOfBlankLines(textField);
- linesToScroll = -linesToScroll;
-
- scroll = (blankLines > linesToScroll) ? blankLines : linesToScroll;
- FldScrollField(textField, scroll, winUp);
-
- // If there were blank lines visible at the end of the field
- // then we need to update the scroll bar.
- if (blankLines >= linesToScroll)
- UpdateScrollBar(textID, scrollBarID);
-
- }
- else
- if (linesToScroll > 0)
- FldScrollField(textField, linesToScroll, winDown);
- }
-
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean SetEditFieldText(FormPtr form, UInt16 id, UInt8* text, UInt16 size)
- {
- UInt8* textP;
- VoidHand textH = 0;
- VoidHand hand;
- FieldPtr field;
-
- form = (form == 0) ? FrmGetActiveForm() : form;
- field = FrmGetObjectPtr(form, FrmGetObjectIndex(form, id));
- if (size > 0)
- {
- textH = MemHandleNew(size + 1);
- if (textH == 0)
- return false;
-
- textP = MemHandleLock(textH);
- if (text != 0)
- MemMove(textP, text, size);
- textP[size] = 0;
- MemHandleUnlock(textH);
- }
-
- hand = FldGetTextHandle(field);
- FldSetTextHandle(field, 0);
- if (hand != 0)
- MemHandleFree(hand);
-
- FldSetTextHandle(field, textH);
- return true;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean AppendEditFieldText(UInt16 id, UInt8 *text, UInt16 size)
- {
- UInt8* textP;
- VoidHand textH;
- FieldPtr field;
- UInt32 length = -1;
-
- field = GetObjectPtr(id);
- textH = FldGetTextHandle(field);
- if (textH == 0) {
- textH = MemHandleNew(size + 1);
- length = 0;
- }
- else
- {
- FldSetTextHandle(field, 0);
- if (MemHandleResize(textH, MemHandleSize(textH) + size) != 0)
- {
- FldSetTextHandle(field, textH);
- textH = 0;
- }
- }
-
- if (textH == 0)
- return false;
-
- textP = MemHandleLock(textH);
- if (length == -1)
- length = StrLen((char*)textP);
- MemMove(&textP[length], text, size);
- textP[length + size] = 0;
- MemHandleUnlock(textH);
-
- FldSetTextHandle(field, textH);
- return true;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Err AppRomVersionCompatible(UInt32 requiredVersion, UInt16 launchFlags)
- {
- UInt32 romVersion;
-
- // See if we're on in minimum required version of the ROM or later.
- // The system records the version number in a feature. A feature is a
- // piece of information which can be looked up by a creator and feature
- // number.
- FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
- if (romVersion < requiredVersion) {
- // If the user launched the app from the launcher, explain
- // why the app shouldn't run. If the app was contacted for something
- // else, like it was asked to find a string by the system find, then
- // don't bother the user with a warning dialog. These flags tell how
- // the app was launched to decided if a warning should be displayed.
- if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) == (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) {
- FrmAlert(RomIncompatibleAlert);
-
- // Pilot 1.0 will continuously relaunch this app unless we switch to
- // another safe one. The sysFileCDefaultApp is considered "safe".
- if (romVersion < 0x02000000) {
- AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
- }
- }
-
- return sysErrRomIncompatible;
- }
-
- return 0;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean AppStart(void)
- {
- Err err;
-
- // Figure out which serial driver is on the system (old or new)
- err = FtrGet(sysFileCSerialMgr, 1, &gUseNewSerialManager);
- if (err) {
- gUseNewSerialManager = 0;
- }
-
- // Find the serial library.
- err = PrvSerLibFind(&gRefNum);
-
- // If error loading library or library was not found
- if ((err != 0) || (gRefNum == 0)) {
- gRefNum = 0;
- }
-
- // Open the library / serial port
- else
- {
- err = PrvSerOpen(gRefNum, sysFileCVirtIrComm, 9600, &gRefNum);
- if (err != 0) {
- gRefNum = 0;
- }
- }
-
- return (err == 0);
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void AppReceived(UInt8 *str, UInt32 size)
- {
- /*UInt32 i, j = 0;
-
- for (i = 0 ; i < size ; i++) {
- if (str[i] == '\r') {
- if (str[i + 1] == '\n') {
- i++;
- }
- str[j++] = '\n';
- }
- else {
- if (((str[i] >= ' ') && (str[i] < 0x80)) || (str[i] == '\n')) {
- str[j++] = str[i];
- }
- }
- }
-
- size = j;
-
- AppendEditFieldText(MainTextField, str, size);
- FldRecalculateField(GetObjectPtr(MainTextField), true);
- HandleTextScroll(100, MainTextField, MainTextScrollBar);
- UpdateScrollBar(MainTextField, MainTextScrollBar);
- */
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void AppReceive(void)
- {
- UInt8 buffer[30];
- Err err;
- UInt32 count;
- UInt32 recvd = 1; // Init to non-zero value for first pass thru while loop
-
- #if 1
- // Check to see if any data has arrived
- err = PrvSerReceiveCheck(gRefNum, &count);
-
- // Read in the data a buffer (maximum) at a time
- while (!err && count && recvd) {
- recvd = PrvSerReceive(gRefNum, buffer, min(sizeof(buffer), count), 0, &err);
- if (recvd) {
- AppReceived(buffer, recvd);
- count -= recvd;
- }
- }
- #else
- // Read in all data that may have arrived
- // This variation tests that a bug in SerReceive (returns data even if less than requested)
- // is fixed, but is not compatible with versions of the ROM without the bug fix.
- while (recvd) {
- recvd = PrvSerReceive(gRefNum, buffer, sizeof(buffer), 0, &err);
- if (recvd) {
- AppReceived(buffer, recvd);
- }
- }
- #endif
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void MainViewInit(void)
- {
- FormPtr form;
-
- // Get a pointer to the main form.
- form = FrmGetActiveForm();
-
- // Draw the form.
- FrmDrawForm(form);
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void MainViewSend(char *str)
- {
- Err err;
- // char *str = FldGetTextPtr(GetObjectPtr(MainInputField));
- UInt32 count, size = 0;
- UInt8 buffer[101];
-
- if (str != 0) {
- size = StrLen(str);
- }
- if (size == 0) {
- // Send out the 4K test buffer
- PrvSerSend(gRefNum, &gTestBuffer[0], sizeof(gTestBuffer), &err);
- return;
- }
-
- if (size > 100) {
- size = 100;
- }
- MemMove(buffer, str, size);
- buffer[size] = '\r';
- size++;
-
- count = PrvSerSend(gRefNum, buffer, size, &err);
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean MainViewHandleButton(EventPtr event)
- {
- Boolean handled = true;
-
- switch (event->data.ctlSelect.controlID) {
- case MainStopButton:
- MainViewSend(kStopCmd);
- break;
-
- case MainPlayPauseButton:
- MainViewSend(kPlayCmd);
- break;
-
- case MainEjectButton:
- MainViewSend(kEjectCmd);
- break;
-
- case MainPrevButton:
- MainViewSend(kPrevCmd);
- break;
-
- case MainNextButton:
- MainViewSend(kNextCmd);
- break;
-
- case MainRewindButton:
- MainViewSend(kRewindCmd);
- break;
-
- case MainFFButton:
- MainViewSend(kFastFwdCmd);
- break;
-
- default:
- handled = false;
- break;
- }
-
- return handled;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean MainViewHandlePopup(EventPtr event)
- {
- Boolean handled = true;
- // FieldPtr field;
- // char buffer[30];
-
- switch (event->data.popSelect.listID) {
- /* case MainCmdsList:
- // Get corresponding mdm cmd text and put it into the send 'buffer'
- SysStringByIndex(ModemCmdsStringList, event->data.popSelect.selection, buffer, sizeof(buffer));
- SetEditFieldText(0, MainInputField, (unsigned char*)buffer, StrLen(buffer));
- field = GetObjectPtr(MainInputField);
- FldRecalculateField(field, true);
- FrmSetFocus(FrmGetActiveForm(), FrmGetObjectIndex(FrmGetActiveForm(), MainInputField));
- FldSetSelection(field, 0, FldGetTextLength(field));
- break;
- */
- default:
- handled = false;
- break;
- }
-
- return handled;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean MainViewHandleEvent(EventPtr event)
- {
- Boolean handled = false;
- FormPtr form;
-
- switch (event->eType) {
- case ctlSelectEvent: // A control button was pressed and released.
- handled = MainViewHandleButton(event);
- break;
-
- case popSelectEvent: // An item was selected from a popup list
- handled = MainViewHandlePopup(event);
- break;
-
- case keyDownEvent:
- switch(event->data.keyDown.chr)
- {
- case pageUpChr:
-
- handled = true;
- break;
-
- case pageDownChr:
-
- handled = true;
- break;
- }
- break;
-
- case sclRepeatEvent:
- // HandleTextScroll(event->data.sclRepeat.newValue - event->data.sclRepeat.value, MainTextField, MainTextScrollBar);
- break;
-
- case frmOpenEvent: // The form was told to open.
- // Initialize the main form.
- MainViewInit();
- handled = true;
- break;
-
- case menuEvent: // A menu item was selected.
- // The “get info” being the only menu item, display the info form.
- // First clear the menu status from the display.
- MenuEraseStatus(0);
-
- // Load the info form, then display it.
- form = FrmInitForm(IrCommTestInfoForm);
- FrmDoDialog(form);
-
- // Delete the info form.
- FrmDeleteForm(form);
- handled = true;
- break;
- }
-
- return handled;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static Boolean AppHandleEvent(EventPtr event)
- {
- FormPtr form;
- Int16 formID;
- Boolean handled = false;
-
- if (event->eType == frmLoadEvent) {
- // Load the form resource specified in the event then activate the form.
- formID = event->data.frmLoad.formID;
- form = FrmInitForm(formID);
- FrmSetActiveForm(form);
-
- // Set the event handler for the form. The handler of the currently
- // active form is called by FrmDispatchEvent each time it receives an event.
- switch (formID) {
- case MainForm:
- FrmSetEventHandler(form, MainViewHandleEvent);
- break;
-
- #ifdef FIXME
- case kTraceDialogID:
- FrmSetEventHandler(form, TraceHandleEvent);
- break;
- #endif
- }
-
- handled = true;
- }
-
- return handled;
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void AppEventLoop(void)
- {
- EventType event;
- UInt16 error;
-
- do {
- // Get the next available event.
- EvtGetEvent(&event, sysTicksPerSecond);
- AppReceive();
-
- if (!SysHandleEvent(&event))
- if (!MenuHandleEvent(0, &event, &error))
- if (!AppHandleEvent(&event))
- FrmDispatchEvent(&event);
- } while (event.eType != appStopEvent);
- }
-
- /* --------------------------------------------------------------------------- */
-
- static void AppStop(void)
- {
- if (gRefNum != 0) {
- PrvSerClose(gRefNum);
- }
-
- FrmCloseAllForms();
- }
-
- /* --------------------------------------------------------------------------- */
-
- UInt32 PilotMain(UInt16 cmd, Ptr /*cmdPBP*/, UInt16 launchFlags)
- {
- Err err;
-
- // This app makes use of PalmOS 3.0 features. It will crash if
- // run on an earlier version of PalmOS. Detect and warn if this happens,
- // then exit.
- if ((err = AppRomVersionCompatible(version30, launchFlags)) != 0)
- return err;
-
- switch (cmd)
- {
- // Check for a normal launch.
- case sysAppLaunchCmdNormalLaunch:
- // Initialize the application's global variables and database.
- if (AppStart())
- {
- // Start the first form.
- FrmGotoForm(MainForm);
-
- // Start the event loop.
- AppEventLoop();
- }
-
- // Clean up before exiting the application.
- AppStop();
- break;
-
- case sysAppLaunchCmdSaveData:
- FrmSaveAllForms();
- break;
- }
-
- return 0;
- }
-